package spine.starling {
	import spine.AnimationState;
	import spine.AnimationStateData;
	import spine.SkeletonData;

public class SkeletonAnimationSprite extends SkeletonSprite {
	public var states:Vector.<AnimationState> = new Vector.<AnimationState>();

	private var animationEvents:Vector.<Object> = new Vector.<Object>();

	public function SkeletonAnimationSprite (skeletonData:SkeletonData) {
		super(skeletonData);
		addAnimationState();
	}

	/**
	 * 让当前的动画时间轴前进 time秒
	 */
	override public function advanceTime (time:Number) : void {
		super.advanceTime(time);
		for each (var state:AnimationState in states) {
			state.update(time);
			state.apply(skeleton);
			
			var currentTime:Number = state.time;

			if (state.loop)
				currentTime %= state.animation.duration;

			for each (var ae:Object in animationEvents)
			{
				//var cls:Class = getDefinitionByName("avatar.PlayerModel") as Class
				if (ae.animation == state.animation.name && ae.time <= currentTime && ae.time > currentTime - time)//时间轴刚好跨越/到达所定义的时间点
				{
					/*if (ae.animation == "skill01" && this.parent is cls) 
					{
						trace("event: " + ae.animation + " " + currentTime + " " + ae.time + " " + time);
						
						trace("是够完成:"+state.isComplete);
					}*/
					//trace("动作ae.animation: "+ ae.animation +" 事件ae.event: "+ae.event);
					dispatchEvent(new AnimationEvent(ae.event));
				}
			}
		}
		skeleton.updateWorldTransform();
	}
	
	
	/**
	 * 在某种动作动画的某个时间添加一个事件
	 * 如addAnimationEvent("attack01",0.3,"hit");//是在攻击动画的0.3秒时，添加hit事件
	 */
	public function addAnimationEvent(animation:String, time:Number, event:String) : void {
		if(hasAnimationEvent(animation,time,event) == false)
		{
			animationEvents.push( { animation:animation, time: time, event: event } );
		}
	}
	
	public function hasAnimationEvent(animation:String, time:Number, event:String):Boolean
	{
		var obj:Object;
		for(var i:int = 0 ; i < animationEvents.length ; i++)
		{
			obj = animationEvents[i];
			if(obj.animation == animation && obj.time == time && obj.event == event)
			{
				return true;
			}
		}
		return false;
	}
	
	public function removeAnimationEvent(animation:String, time:Number, event:String):Object
	{
		var obj:Object;
		for(var i:int = 0 ; i < animationEvents.length ; i++)
		{
			obj = animationEvents[i];
			if(obj.animation == animation && obj.time == time && obj.event == event)
			{
				animationEvents.splice(i,1);
				return obj;
			}
		}
		return null;
	}
	
	public function addAnimationState (stateData:AnimationStateData = null) : void {
		if (!stateData)
			stateData = new AnimationStateData(skeleton.data);
		states.push(new AnimationState(stateData));
	}

	public function setAnimationStateData (stateData:AnimationStateData, stateIndex:int = 0) : void {
		if (stateIndex < 0 || stateIndex >= states.length)
			throw new ArgumentError("stateIndex out of range.");
		if (!stateData)
			throw new ArgumentError("stateData cannot be null.");
		states[stateIndex] = new AnimationState(stateData);
	}

	public function setMix (fromAnimation:String, toAnimation:String, duration:Number, stateIndex:int = 0) : void {
		if (stateIndex < 0 || stateIndex >= states.length)
			throw new ArgumentError("stateIndex out of range.");
		states[stateIndex].data.setMixByName(fromAnimation, toAnimation, duration);
	}

	public function setAnimation (name:String, loop:Boolean, stateIndex:int = 0) : void {
		if (stateIndex < 0 || stateIndex >= states.length)
			throw new ArgumentError("stateIndex out of range.");
		states[stateIndex].setAnimationByName(name, loop);
	}

	public function addAnimation (name:String, loop:Boolean, delay:Number = 0, stateIndex:int = 0) : void {
		if (stateIndex < 0 || stateIndex >= states.length)
			throw new ArgumentError("stateIndex out of range.");
		states[stateIndex].addAnimationByName(name, loop, delay);
	}

	public function clearAnimation (stateIndex:int = 0) : void {
		if (stateIndex < 0 || stateIndex >= states.length)
			throw new ArgumentError("stateIndex out of range.");
		states[stateIndex].clearAnimation();
	}
	
	/**
	 *按百分比设置当前的动画时间 
	 * timePercent:0~100这间的整数
	 */
	public function gotoTimePercent(timePercent:int):void
	{
		for each (var state:AnimationState in states) {
			state.time = timePercent/100*totalTime;
		}
		advanceTime(0.1);
	}
	
	/**
	 * 当前状态的动画总时长，返回秒
	 */
	public function get totalTime():Number
	{
		for each (var state:AnimationState in states) {
			return state.animation.duration;
		}
		return 0;
	}
}

}
